import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/prisma"; import { loadFileData } from "@/lib/storage"; export async function GET( request: NextRequest, { params }: { params: { id: string } } ) { const file = await prisma.file.findUnique({ where: { id: params.id } }); if (!file) { return NextResponse.json({ error: "File not found" }, { status: 404 }); } // Contenu chiffré au repos → on déchiffre en mémoire puis on sert (avec support Range). const data = await loadFileData(file.storagePath, file.isEncrypted); const fileSize = data.length; const range = request.headers.get("range"); if (range) { const parts = range.replace(/bytes=/, "").split("-"); const start = parseInt(parts[0], 10); const end = parts[1] ? parseInt(parts[1], 10) : Math.min(start + 1024 * 1024 - 1, fileSize - 1); const chunk = data.subarray(start, end + 1); return new NextResponse(new Uint8Array(chunk), { status: 206, headers: { "Content-Range": `bytes ${start}-${end}/${fileSize}`, "Accept-Ranges": "bytes", "Content-Length": String(chunk.length), "Content-Type": file.mimeType, "Cache-Control": "private, max-age=3600", }, }); } return new NextResponse(new Uint8Array(data), { headers: { "Content-Length": String(fileSize), "Content-Type": file.mimeType, "Accept-Ranges": "bytes", "Cache-Control": "private, max-age=3600", }, }); }